package com.tjtt.tdm.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.exception.DaoException;
import com.tjtt.tdm.exception.ServiceException;
import com.tjtt.tdm.system.bean.Role;
import com.tjtt.tdm.system.bean.Rolemenu;
import com.tjtt.tdm.system.bean.UserRole;
import com.tjtt.tdm.system.dao.RoleDao;
import com.tjtt.tdm.system.dao.RoleMenuDao;
import com.tjtt.tdm.system.dao.UserDao;
import com.tjtt.tdm.system.service.RoleService;

public class RoleServiceImpl implements RoleService {
	private static final Log log = LogFactory.getLog(RoleServiceImpl.class);
	private RoleDao roleDao;
	private RoleMenuDao roleMenuDao;
	private UserDao userDao;
	private UserRole userRole;
	

	public UserRole getUserRole() {
		return userRole;
	}

	public void setUserRole(UserRole userRole) {
		this.userRole = userRole;
	}

	public void delRole(Long roleid) throws ServiceException{
		try {
			Role delRole = roleDao.get(roleid);
			roleMenuDao.removeByParams(Rolemenu.class,"roleid", new Long[]{roleid});
			roleDao.remove(delRole);
		} catch (DaoException e) {
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
	}
	
	public List<Role> getRolesAll() throws ServiceException{
		try {
			return roleDao.getAll();
		} catch (DaoException e) {
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
	}
	
	public void saveRole(Role role) throws ServiceException{
		try {
			roleDao.save(role);
		} catch (DaoException e) {
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
	}
	
	public void updateRole(Role role) throws ServiceException {
		try {
			Role updateRole = roleDao.get(role.getRoleid());
			updateRole.setRolename(role.getRolename());
			updateRole.setRoledesc(role.getRoledesc());
			updateRole.setRolelabel(role.getRolelabel());
			roleDao.update(updateRole);
		} catch (DaoException e) {
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
	}
	
	public List<Role> findRoles(HashMap<String, Object> paramMap,long totalCount, long start,
			long limit, String sort, String dir) throws ServiceException {
		
		if (start > totalCount) {
			start = totalCount - (totalCount % limit);
		}
		try {
			return roleDao.findByProperties(start, limit, sort, dir, paramMap);
		} catch (DaoException e) {
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
	}
	
	public Long getTotalCount(HashMap<String, Object> paramMap)
			throws ServiceException {
		try {
			return roleDao.getTotalCount(paramMap);
		} catch (DaoException e) {
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
	
	}
	public boolean findRoleMenuByMenuIdAndRoleId(Long roleId,Long menuId) throws ServiceException{
		String[] names = new String[]{"roleid","menuid"};
		Long[] values = new Long[]{roleId,menuId};
		try {
			List<Rolemenu> roleMenus = roleMenuDao.findByProperties(names, values);
			return roleMenus.size() > 0;
		} catch (DaoException e) {
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
	}
	
	public void updateRoleMenuByRoleId(Long roleId,List<Long> menuIds) throws ServiceException{
		try {
			roleMenuDao.removeByParams(Rolemenu.class,"roleid", new Long[]{roleId});  //根据roleId删除角色表中记录
			
			for (Long menuId : menuIds) {
				Rolemenu roleMenu = new Rolemenu();
				roleMenu.setMenuid(menuId);
				roleMenu.setRoleid(roleId);
				roleMenuDao.save(roleMenu);
			}
		} catch (DaoException e) {
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
	}
	
	public int findRoleMenuByMenuIdsAndRoleId(Long roleId,Long menuParentId) throws ServiceException{
		try {
			return roleMenuDao.findRoleMenuByMenuIdsAndRoleId(roleId, menuParentId).size();
		} catch (DaoException e) {
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
	}
	
	public List<Rolemenu> getRolemenuByRoleId(Long roleId) throws ServiceException{
		try {
			return roleMenuDao.findByProperty("roleid", roleId);
		} catch (DaoException e) {
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
	}
	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}
	public void setRoleMenuDao(RoleMenuDao roleMenuDao) {
		this.roleMenuDao = roleMenuDao;
	}

	
	public List<Role> findAllRole() throws ServiceException {
		try {
			List<Role> list=roleDao.getAll();
			return list;
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
	}

	
	public Role get(Long roleId) throws ServiceException {
		try {
			return roleDao.get(roleId);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
	}

	//通过主键id查询角色信息
	public Role findRoleById(long roleid) {
		return roleDao.getRoleById(roleid);
	}
//---------------------------------ZXM-----------------------------------------
	//系统角色树形展示
	public List<Role> findRoleTree(Map filterMap) throws ServiceException {
		try{
			return roleDao.findRoleTree(filterMap);
		}catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage(),e);
		}
	}

	//查询角色名称是否存在
	/*public PageResult findRoleByNames(String rolename,Map<String, String> filterMap) throws ServiceException{
		try {
			return roleDao.findRoleByName(rolename, filterMap);
		} catch (Exception e) {			
			e.printStackTrace();
			throw new ServiceException(e.getMessage(),e);
		}
	}
*/
	
	public Role findRoleByName(String rName) {
		return this.roleDao.findRoleByName(rName);
	}

	//查询角色下用户
	public PageResult searchUserByRole(Long roleid, Map<String, String> filterMap) {
		return this.roleDao.searchUserByRole(roleid, filterMap);		
	}
	public UserRole savaUserToRole(UserRole userRole) {		
		return this.roleDao.saveUserToRole(userRole);
	}
	
	public PageResult queryUserList(Long roleid, Map<String, String> filterMap){
		return this.roleDao.queryUserList(roleid, filterMap);
	}

	
	public List queryListBySql(String sql) {
		return this.roleDao.queryListBySql(sql);
	}

	//根据角色查询用户
	public PageResult queryUserByRole(String  roleid, Map<String, String> filterMap){
		return this.roleDao.queryUserByRole(roleid, filterMap);
	}


	public void exeSql(String sql){
		try {
			roleDao.executeSQL(sql);
		} catch (DaoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
